package grouplife::Controller::Login;
use Moose;
use namespace::autoclean;
use Catalyst qw/
Session
Session::Store::FastMmap
Session::State::Cookie
/;


BEGIN {extends 'Catalyst::Controller'; }
__PACKAGE__->config( 'Plugin::ConfigLoader' => { file => 'grouplife.yml' } );

=head1 NAME

grouplife::Controller::Login - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut


sub index :Path :Args(0) {
	my ($self, $c) = @_;

# Get the username and password from form
	my $email = $c->request->params->{email};
	my $password = $c->request->params->{create_password};
	my $remember_me = $c->request->params->{remember_me};

# If the username and password values were found in form
	if ($email && $password) {
# Attempt to log the user in
		if ($c->authenticate({ email_address => $email, active => 'Y',
					password => $password  } )) {
			my $user = $c->model('DB::User')->search({email_address => $email})->first;
			$c->session->{login_user} = $user;			
# If successful, then let them use the application
			if(defined($c->flash->{redirect_after_login})){
				$c->response->redirect($c->flash->{redirect_after_login});
			}else{
				$c->response->redirect($c->uri_for('/'));
			}
			return;
		} else {
# Set an error message
			$c->stash(error_msg => "Email或密码不正确");
		}
	}
	
# If either of above don't work out, send to the login page
	my $taobao_oauth_url = $c->config->{taobao_oauth_url};
	my $redirect_uri = $c->config->{redirect_uri};
        my $response_type = $c->config->{response_type};
        my $scope = $c->config->{scope};
        my $view = $c->config->{view};
        my $app_key = $c->config->{app_key};

	my $taobao_login = $taobao_oauth_url . "?response_type=" . $response_type . "&client_id=" . $app_key . "&redirect_uri=" . $redirect_uri . "&scope=" . $scope . "&view=" . $view;
	
	my $renren_oauth = $c->config->{renren_oauth};
        my $renren_redirect_uri = $c->config->{renren_redirect_uri};
        my $renren_response_type = $c->config->{renren_response_type};
        my $renren_scope = $c->config->{renren_scope};
        my $renren_view = $c->config->{renren_view};
        my $renren_app_key = $c->config->{renren_app_key};

        my $renren_login = $renren_oauth . "?client_id=" . $renren_app_key . "&redirect_uri=" . $renren_redirect_uri . "&response_type=" . $renren_response_type;
	$c->stash(taobao_login => $taobao_login);
	$c->stash(renren_login => $renren_login);
	$c->stash(title => '登录');
	$c->stash(css => 'denglu.css'),
	$c->stash(javascript => '/user/denglu.js'),
	$c->stash(template => 'login.tt2');
}
=head1 AUTHOR

lijie,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
